<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-pic"></i> 每日任务
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box" style="margin-bottom:50px">
                <el-col :span="6">
                    <el-button
                        type="danger"
                        icon="el-icon-delete"
                        class="handle-del mr10"
                        @click="delAllSelection"
                    >批量删除</el-button>
                    <el-button
                        type="primary"
                        icon="el-icon-circle-plus-outline"
                        class="handle-del mr10"
                        @click="handleAdd"
                    >添加每日任务</el-button>
                </el-col>
                <el-col :span="12">
                    <el-select v-model="query.status" placeholder="状态" class="handle-select mr10">
                        <el-option label="全部" value></el-option>
                        <el-option key="1" label="正常" value="1"></el-option>
                        <el-option key="2" label="禁用" value="2"></el-option>
                    </el-select>
                    <el-input v-model="query.name" placeholder="任务名称" class="handle-input mr10"></el-input>
                    <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                </el-col>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                v-loading="loading"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="70" sortable align="center"></el-table-column>
                <el-table-column prop="name" label="任务名称"></el-table-column>
                <el-table-column width="180" prop="adv_type_name" label="广告类型"></el-table-column>
                <el-table-column prop="ad_pid" label="广告位ID"></el-table-column>
                <el-table-column label="任务图标" align="center">
                    <template slot-scope="scope">
                        <el-image
                            class="table-td-thumb"
                            :src="scope.row.show_image"
                            :preview-src-list="[scope.row.show_image]"
                        ></el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="num" label="任务次数"></el-table-column>
                <el-table-column prop="score" label="奖励兑换券"></el-table-column>
                <el-table-column prop="button_name" label="按钮名称"></el-table-column>
                <el-table-column prop="weigh" label="权重(越大靠前)"></el-table-column>
                <el-table-column label="状态" sortable align="center">
                    <template slot-scope="scope">
                        <el-tag
                            :type="scope.row.status==1?'success':'danger'"
                        >{{scope.row.status==1?'正常':'禁用'}}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column width="160" prop="created_at" sortable label="添加时间"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.page"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 添加弹出框 -->
        <el-dialog title="添加每日任务" v-dialogDrag :visible.sync="addVisible" width="580px">
            <el-form ref="add" :model="add" :rules="rules" label-width="100px">
                <el-form-item label="任务名称" prop="name">
                    <el-input v-model="add.name" maxlength="25" minlength="2" placeholder="请输入任务名称" class="form-item"></el-input>
                </el-form-item>
                <el-form-item label="广告类型" prop="adv_type_id">
                     <el-select v-model="add.adv_type_id" filterable placeholder="请选择">
                       <el-option
                         v-for="item in types"
                         :key="item.id"
                         :label="item.name"
                         :value="item.id">
                       </el-option>
                     </el-select>
                </el-form-item>
                <el-form-item label="广告位ID" prop="ad_pid">
                    <el-input v-model="add.ad_pid" maxlength="50" minlength="1" placeholder="请输入App广告位ID" class="form-item"></el-input>
                </el-form-item>
                 <el-form-item label="上传图标" prop="show_image">
                    <el-upload
                      class="avatar-uploader"
                      :action="api.upload_img_url"
                      :show-file-list="false"
                      :on-success="handleAvatarSuccess"
                      :before-upload="beforeAvatarUpload">
                      <img v-if="add.show_image" :src="add.show_image" class="avatar">
                      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="任务次数" prop="num">
                    <el-input v-model="add.num" maxlength="2" minlength="1" placeholder="请输入任务次数" class="form-item"></el-input>
                </el-form-item>
                <el-form-item label="奖励兑换券" prop="score">
                    <el-input v-model="add.score" maxlength="10" minlength="1" placeholder="请输入奖励兑换券" class="form-item"></el-input>
                </el-form-item>
                <el-form-item label="按钮名称" prop="button_name">
                    <el-input v-model="add.button_name" maxlength="10" minlength="2" placeholder="请输入按钮名称" class="form-item"></el-input>
                </el-form-item>
                <el-form-item label="任务描述" prop="desc">
                    <el-input type="textarea" v-model="add.desc"></el-input>
                </el-form-item>
                 <el-form-item label="任务权重" prop="weigh">
                   <el-input v-model="add.weigh" maxlength="2" minlength="1" placeholder="请输入任务权重" class="form-item"></el-input>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                   <el-select v-model="add.status" placeholder="请选择状态">
                       <el-option label="正常" value="1"></el-option>
                       <el-option label="禁用" value="2"></el-option>
                   </el-select>
               </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd('add')">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑每日任务" v-dialogDrag :visible.sync="editVisible" width="580px">
            <el-form ref="edit" :model="edit" :rules="rules" label-width="100px">
                <el-form-item label="任务名称" prop="name">
                    <el-input v-model="edit.name" maxlength="25" minlength="2" placeholder="请输入任务名称" class="form-item"></el-input>
                </el-form-item>
                <el-form-item label="广告类型" prop="adv_type_id">
                     <el-select v-model="edit.adv_type_id" filterable placeholder="请选择">
                       <el-option
                         v-for="item in types"
                         :key="item.id"
                         :label="item.name"
                         :value="item.id">
                       </el-option>
                     </el-select>
                </el-form-item>
                <el-form-item label="广告位ID" prop="ad_pid">
                    <el-input v-model="edit.ad_pid" maxlength="50" minlength="1" placeholder="请输入App广告位ID" class="form-item"></el-input>
                </el-form-item>
                 <el-form-item label="上传图标" prop="show_image">
                    <el-upload
                      class="avatar-uploader"
                      :action="api.upload_img_url"
                      :show-file-list="false"
                      :on-success="handleAvatarSuccess"
                      :before-upload="beforeAvatarUpload">
                      <img v-if="edit.show_image" :src="edit.show_image" class="avatar">
                      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="任务次数" prop="num">
                    <el-input v-model="edit.num" maxlength="2" minlength="1" placeholder="请输入任务次数" class="form-item"></el-input>
                </el-form-item>
                <el-form-item label="奖励兑换券" prop="score">
                    <el-input v-model="edit.score" maxlength="10" minlength="1" placeholder="请输入奖励兑换券" class="form-item"></el-input>
                </el-form-item>
                <el-form-item label="按钮名称" prop="button_name">
                    <el-input v-model="edit.button_name" maxlength="10" minlength="2" placeholder="请输入按钮名称" class="form-item"></el-input>
                </el-form-item>
                <el-form-item label="任务描述" prop="desc">
                    <el-input type="textarea" v-model="edit.desc"></el-input>
                </el-form-item>
                 <el-form-item label="任务权重" prop="weigh">
                   <el-input v-model="edit.weigh" maxlength="2" minlength="1" placeholder="请输入任务权重" class="form-item"></el-input>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                   <el-select v-model="edit.status" placeholder="请选择状态">
                       <el-option label="正常" value="1"></el-option>
                       <el-option label="禁用" value="2"></el-option>
                   </el-select>
               </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit('edit')">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
  name: 'type',
  data () {
    return {
      query: {
        status: '',
        sort: '',
        name: '',
        page: 1,
        pageSize: 10
      },
      loading: false,
      tableData: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      addVisible: false,
      pageTotal: 0,
      edit: {},
      add: {
        name: '',
        adv_type_id: '',
        ad_pid: '',
        image: '',
        show_image: '',
        num: '',
        score: '',
        button_name: '',
        desc: '',
        weigh: '',
        status: ''
      },
      types: [],
      idx: -1,
      id: -1,
      getList: function () {
        let that = this;
        this.api.get('getDayTask', this.query, res => {
          for (let i in res.data.list) {
            res.data.list[i].status = res.data.list[i].status + '';
          }
          this.types = res.data.types;
          that.tableData = res.data.list;
          that.pageTotal = res.data.pageTotal;
        }, undefined, undefined, this);

      },
      delDayTask: function (ids, msg) {
        let that = this;
        if (!ids || ids.length < 1) {
          this.$message.error('请选择删除的数据');
          return false;
        }
        // 二次确认删除
        this.$confirm('确定要删除吗？', '提示', {
          type: 'warning'
        }).then(() => {
          that.api.delDayTask(ids, function (response) {
            that.multipleSelection = [];
            that.query.page = 1;
            that.getList();
            that.$message.success(msg);
          });
        }).catch(() => { });
      },
      rules: {
        name: [
          { required: true, message: '请输入任务名称', trigger: 'blur' },
        ],
        adv_type_id: [
          { required: true, message: '请选择广告类型', trigger: 'change' }
        ],
        ad_pid: [
          { required: true, message: '请输入App广告位ID', trigger: 'blur' },
        ],
        show_image: [
          { required: true, message: '请上传任务图标', trigger: 'blur' },
        ],
        num: [
          { required: true, message: '请输入任务次数', trigger: 'blur' },
        ],
        score: [
          { required: true, message: '请输入奖励兑换券', trigger: 'blur' },
        ],
        button_name: [
          { required: true, message: '请输入按钮名称', trigger: 'blur' },
        ],
        desc: [
          { required: true, message: '请输入任务描述', trigger: 'blur' },
        ],
        weigh: [
          { required: true, message: '请输入任务权重', trigger: 'blur' },
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ]
      }
    };
  },
  created () {
    this.getList();
  },
  methods: {
    handleAvatarSuccess(res, file) {
        this.add.show_image = res.data.url;
        this.add.image = res.data.path;
        this.edit.show_image = res.data.url;
        this.edit.image = res.data.path;
    },
    beforeAvatarUpload(file) {
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isLt2M;
    },
    // 触发搜索按钮
    handleSearch () {
      this.$set(this.query, 'page', 1);
      this.getList();
    },
    // 删除操作
    handleDelete (index, row) {
      this.delDayTask([row.id], '删除成功');
    },
    // 多选操作
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },
    delAllSelection () {
      const length = this.multipleSelection.length;
      let str = '';
      this.delList = this.delList.concat(this.multipleSelection);
      let ids = [];
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + ' ';
        ids.push(this.multipleSelection[i].id);
      }
      this.delDayTask(ids, `删除了${str}`);
    },
    // 编辑操作
    handleEdit (index, row) {
      this.idx = index;
      this.edit = row;
      this.editVisible = true;
    },
    // 保存编辑
    saveEdit (edit) {
      let that = this;
      this.$refs[edit].validate((valid) => {
            if (!valid) {
              console.log('error submit!!');
              return false;
            } else {
              that.api.editDayTask(that.edit, function () {
                  that.$message.success('保存成功');
                  that.editVisible = false;
                  that.getList();
              });
            }
      });
    },
    /***
     * 添加操作
     */
    handleAdd () {
      this.add = {
           name: '',
           adv_type_id: '',
           ad_pid: '',
           image: '',
           show_image: '',
           num: '',
           score: '',
           button_name: '',
           desc: '',
           weigh: '',
           status: ''
      };
      this.addVisible = true;
    },
    /***
     * 保存添加
     */
    saveAdd (add) {
      let that = this;
      this.$refs[add].validate((valid) => {
          if (!valid) {
            console.log('error submit!!');
            return false;
          } else {
            that.api.addDayTask(that.add, function () {
                that.$message.success('添加成功');
                that.addVisible = false;
                that.getList();
            });
          }
      });
    },
    // 分页导航
    handlePageChange (val) {
      this.$set(this.query, 'page', val);
      this.getList();
    }

  }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}
.form-item {
    width: 215px;
}
.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
  }
</style>